#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_constrain1a.info <<'%EOF%'
   geoopt_constrain1a
   ------------------
   Molecule:         C2H6
   Wave Function:    N/A
   Test Purpose:     Demonstrates a run were nothing but determination of
                     redundant internal coordinates is performed. This is
                     needed to get the coordinate numbers required for
                     constraints
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_constrain1a.mol <<'%EOF%'
BASIS
STO-3G
Determination of redundant internal coordinates

Atomtypes=2
Charge=6.0 Atoms=2
C     0.0000000000        0.0000000000        1.7547876282             *
C     0.0000000000        0.0000000000       -1.7547876282             *
Charge=1.0 Atoms=6
H     1.6808445513        0.9704360937        2.4445545504             *
H    -1.6808445513        0.9704360937        2.4445545504             *
H     0.0000000000       -1.9408721894        2.4445545504             *
H     1.6808445513       -0.9704360937       -2.4445545504             *
H    -1.6808445513       -0.9704360937       -2.4445545504             *
H     0.0000000000        1.9408721894       -2.4445545504             *
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_constrain1a.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
*OPTIMIZE
.FINDRE
**WAVE FUNCTION
.HF
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_constrain1a.check
cat >>geoopt_constrain1a.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

# Determination
CRIT1=`$GREP "Determination of redundant internal will be performed\." $log | wc -l`
CRIT2=`$GREP "No geometry optimization will be done\, other keywords will be ignored\!\!" $log | wc -l`
TEST[1]=`expr	$CRIT1 \+ $CRIT2`
CTRL[1]=2
ERROR[1]="INTERNAL COORDINATE DETERMINATION NOT SET UP CORRECTLY"

# Symmetry
CRIT1=`$GREP "Symmetry class found\: D\(3d\)" $log | wc -l`
CRIT2=`$GREP "The following symmetry elements were found\: * X * YZ" $log | wc -l`
TEST[2]=`expr	$CRIT1 \+ $CRIT2`
CTRL[2]=2
ERROR[2]="SYMMETRY NOT CORRECT"

# Geometry
CRIT1=`$GREP ". * 6\.00000000 *\-*0*\.00000000 *\-*0*\.00000000 *  0*\.9285936." $log | wc -l`
CRIT2=`$GREP ". * 6\.00000000 *\-*0*\.00000000 *\-*0*\.00000000 *\-0*\.9285936." $log | wc -l`
CRIT3=`$GREP ". * 1\.00000000 *\-*0*\.00000000 *  \-1\.027065.. *\-*1\.2936025." $log | wc -l`
CRIT4=`$GREP ". * 1\.00000000 *\-*0*\.00000000 *    1\.027065.. *\-*1\.2936025." $log | wc -l`
CRIT5=`$GREP ". * 1\.00000000 *   0*\.8894646. *   0*\.5135326. *\-*1\.2936025." $log | wc -l`
CRIT6=`$GREP ". * 1\.00000000 * \-0*\.8894646. *   0*\.5135326. *\-*1\.2936025." $log | wc -l`
CRIT7=`$GREP ". * 1\.00000000 *   0*\.8894646. * \-0*\.5135326. *\-*1\.2936025." $log | wc -l`
CRIT8=`$GREP ". * 1\.00000000 * \-0*\.8894646. * \-0*\.5135326. *\-*1\.2936025." $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8`
CTRL[3]=8
ERROR[3]="GEOMETRY NOT READ CORRECTLY"

# Bonds
CRIT1=`$GREP "1 * Regular bond * 1 * 2 * 1\.85719 Ang\." $log | wc -l`
CRIT2=`$GREP "2 * Regular bond * 1 * . * 1\.09000 Ang\." $log | wc -l`
CRIT3=`$GREP "3 * Regular bond * 1 * . * 1\.09000 Ang\." $log | wc -l`
CRIT4=`$GREP "4 * Regular bond * 1 * . * 1\.09000 Ang\." $log | wc -l`
CRIT5=`$GREP "5 * Regular bond * 2 * . * 1\.09000 Ang\." $log | wc -l`
CRIT6=`$GREP "6 * Regular bond * 2 * . * 1\.09000 Ang\." $log | wc -l`
CRIT7=`$GREP "7 * Regular bond * 2 * . * 1\.09000 Ang\." $log | wc -l`
TEST[4]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7`
CTRL[4]=7
ERROR[4]="BONDS NOT DETERMINED CORRECTLY"

# Angles
CRIT1=`$GREP "20 * Regular angle * 2 * 1 * . * 109\.565 deg\." $log | wc -l`
CRIT2=`$GREP "21 * Regular angle * 2 * 1 * . * 109\.565 deg\." $log | wc -l`
CRIT3=`$GREP "22 * Regular angle * 2 * 1 * . * 109\.565 deg\." $log | wc -l`
CRIT4=`$GREP "23 * Regular angle * . * 1 * . * 109\.378 deg\." $log | wc -l`
CRIT5=`$GREP "24 * Regular angle * . * 1 * . * 109\.378 deg\." $log | wc -l`
CRIT6=`$GREP "25 * Regular angle * . * 1 * . * 109\.378 deg\." $log | wc -l`
CRIT7=`$GREP "26 * Regular angle * 1 * 2 * . * 109\.565 deg\." $log | wc -l`
CRIT8=`$GREP "27 * Regular angle * 1 * 2 * . * 109\.565 deg\." $log | wc -l`
CRIT9=`$GREP "28 * Regular angle * 1 * 2 * . * 109\.565 deg\." $log | wc -l`
CRIT10=`$GREP "29 * Regular angle * . * 2 * . * 109\.378 deg\." $log | wc -l`
CRIT11=`$GREP "30 * Regular angle * . * 2 * . * 109\.378 deg\." $log | wc -l`
CRIT12=`$GREP "31 * Regular angle * . * 2 * . * 109\.378 deg\." $log | wc -l`
TEST[5]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12`
CTRL[5]=12
ERROR[5]="ANGLES NOT DETERMINED CORRECTLY"

# Dihedral angles
CRIT1=`$GREP ".. * Dihedral angle * . * . * . * . * (\-| )60\.000 deg\." $log | wc -l`
CRIT2=`$GREP ".. * Dihedral angle * . * . * . * . * (\-| )180\.000 deg\." $log | wc -l`
TEST[6]=`expr	$CRIT1 \+ $CRIT2`
CTRL[6]=9
ERROR[6]="DIHEDRAL ANGLES NOT DETERMINED CORRECTLY"

# Total number of coordinates
CRIT1=`$GREP "Total number of redundant internal coordinates\: * 40" $log | wc -l`
TEST[7]=`expr	$CRIT1`
CTRL[7]=1
ERROR[7]="TOTAL NUMBER OF INTERNAL COORDINATES NOT CORRECT"

# Other parts of Dalton
# ctrl = 12 because of output header
CRIT1=`$GREP "SIRIUS" $log | wc -l`
CRIT2=`$GREP "ABACUS" $log | wc -l`
TEST[8]=`expr	$CRIT1 \+ $CRIT2`
CTRL[8]=12
ERROR[8]="OTHER PARTS OF DALTON SHOULD NOT BE RUN"

PASSED=1
for i in 1 2 3 4 5 6 7 8
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} : ${TEST[i]} .ne. ${CTRL[i]} ; "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
